package com.tjmc.tiomsresource.parser;

import com.tjmc.tiomscore.model.TranPtnHwCrd;
import org.dom4j.Element;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Luc
 * @version 1.0
 * @date 2020-05-11 17:56
 */

@Component("cmCrdParser")
public class CMCrdParser extends AbstractParser<TranPtnHwCrd> {

    public List<TranPtnHwCrd> parseXML(List<Element> objectElements) {
        if(null == objectElements || objectElements.size() == 0) {
            return null;
        }
        List<TranPtnHwCrd> tranPtnHwCrdList = new ArrayList<>();
        int j = 0;
        for(Element objectElement : objectElements) {
            if(++j == PRINT_LOG_SPLIT || j == objectElements.size()) {
                logger.info("解析：" + j + ", size=" + objectElements.size());
            }
            String rmUID = objectElement.attributeValue("rmUID");
            TranPtnHwCrd tranPtnHwCrd = new TranPtnHwCrd();
            tranPtnHwCrd.setRmuid(rmUID);
            List<Element> vElements = objectElement.elements("V");
            if(null == vElements || vElements.size() == 0) {
                continue;
            }
            for(Element vElement : vElements) {
                int i = Integer.parseInt(vElement.attributeValue("i"));
                switch(i) {
                    case 1:
                        tranPtnHwCrd.setNermuid(vElement.getTextTrim());
                        break;
                    case 2:
                        tranPtnHwCrd.setHolderrmuid(vElement.getTextTrim());
                        break;
                    case 3:
                        tranPtnHwCrd.setNativename(vElement.getTextTrim());
                        break;
                    case 4:
                        tranPtnHwCrd.setCardtype(vElement.getTextTrim());
                        break;
                    case 5:
                        tranPtnHwCrd.setCardsubtype(vElement.getTextTrim());
                        break;
                    case 6:
                        tranPtnHwCrd.setSoftwareversion(vElement.getTextTrim());
                        break;
                    case 7:
                        tranPtnHwCrd.setHardwareversion(vElement.getTextTrim());
                        break;
                    case 8:
                        tranPtnHwCrd.setSerialnumber(vElement.getTextTrim());
                        break;
                    case 9:
                        tranPtnHwCrd.setServicestate(vElement.getTextTrim());
                        break;
                    case 10:
                        tranPtnHwCrd.setRole(vElement.getTextTrim());
                        break;
                }
            }
            tranPtnHwCrdList.add(tranPtnHwCrd);
        }
        return tranPtnHwCrdList.size()==0?null:tranPtnHwCrdList;
    }
}
